<?php 
session_start();
include "../_init_.php";

$action= isset($_REQUEST["action"])?$_REQUEST["action"]:'';
switch($action)
{
	case 'clean':
		$imgpaths = $_REQUEST['imgpaths'];
		if($imgpaths)
		{
			foreach($imgpaths as $path)
			{
				unlink(constant('SITE_WEB_DISK_DIR').$path);
			}
		}
	break;
}
$img_path_list = getImagePathList();
foreach($img_path_list as $index=>$imgPath)
{

	$is_refer = false;
	try
	{
		is_rent_refer($imgPath);
		is_rent_gallery_refer($imgPath);
		is_article_refer($imgPath);
		
	}catch(Exception $ex)
	{
	  /*
		if(strpos($imgPath,'1317918257.summaryimage.jpg') !== false)
		{
			echo "kdkd".$ex->getMessage();
		}
		*/
		$is_refer = true;
	}
	if($is_refer === true)
		unset($img_path_list[$index]);
}
$img_path_list = array_values($img_path_list);
function is_rent_refer($imgPath)
{
	$dt = new DataTable(constant('DB_TABLE_RENT'));
	$condition = sprintf("`image`='%s' OR `image1`='%s'",$imgPath,$imgPath);
	$count = $dt->get_count($condition);
	if($count > 0)
		throw new Exception("refered 1");
}
function is_rent_gallery_refer($imgPath)
{
	$dt = new DataTable(constant('DB_TABLE_RENT_IMAGE'));
	$condition = sprintf("`image`='%s'",$imgPath);
	$count = $dt->get_count($condition);
	if($count > 0)
		throw new Exception("refered 2");
}
function is_article_refer($imgPath)
{
	$dt = new DataTable(constant('DB_TABLE_KEY_VALUE'));
	$condition = sprintf("`value` like '%%%s%%'",$imgPath);
	$count = $dt->get_count($condition);
	if($count > 0)
		throw new Exception("refered 3");
}


function getImagePathList()
{
	$image_path_list = array();
	$dir = constant('SITE_WEB_DISK_DIR').'/upload/';
	$dp = opendir($dir);
	if(!$dp)
		throw new Exception('permission no allowed to open dir '.$dir);
	while (($file=readdir($dp)) != false) {
	if($file == '.' || $file == '..')
		continue;
		if(!is_file($dir.$file))
			continue;
	 	$image_path_list[] = '/upload/'.$file;
	}
	closedir($dp);
	return $image_path_list;
}
?>
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>網站管理系統</title>


<LINK REL="stylesheet" HREF="css/style.css" TYPE="text/css">
<LINK REL="stylesheet" HREF="css/main.css" TYPE="text/css">
<script src="js/FancyZoom.js" language="JavaScript" type="text/javascript"></script>
<script src="js/FancyZoomHTML.js" language="JavaScript" type="text/javascript"></script>
<script src="../js/jquery-ui-1.7.3.custom/js/jquery-1.3.2.min.js" language="javascript"></script>
<script language="javascript">
$(function(){
	$('.checkall').click(function(){
		$('.paths').attr('checked',$(this).attr('checked'));
	});
});
</script>
<body onLoad="setupZoom();">

<table width="100%" border="0" class="table2">
  <tr>
    <td>系統管理 - 圖片清理</td>
  </tr>
</table>
<?php if($error_message):?>
<div class="error_message"><?php echo $error_message;?></div>
<?php endif;?>
<?php if($sucess_message):?>
<div class="success_message"><?php echo $sucess_message;?></div>
<?php endif;?>
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
  <tr> 
    <td align="center" valign="top">
    
      <table width="100%" border="0" cellpadding="2" cellspacing="1" class="table_southidc">
        <tr> 
          <td  height="20" class="back_southidc"> <div align="center"><strong>圖片清理</strong></div></td>
        </tr>
        <tr  class="tr_southidc">          
            <td bgcolor="#B0DAFF">
              <form method="post">
              <input type="hidden" name="action" value="clean"/>
              <table width="100%" border="0" align="center" cellpadding="0" cellspacing="2" bordercolor="#0000FF">
               <tr>
              	<td width="50">序號</td>
              	<td width="150"><input type='checkbox' class="checkall" checked="checked"/>&nbsp;All</td>
                <td>圖片</td>
              </tr>
              <?php foreach($img_path_list as $index=>$row):?>
              <tr>
              	<td><?php echo $index+1;?></td>
              	<td><input type='checkbox' name="imgpaths[]" value="<?php echo $row;?>" checked="checked" class="paths"/></td>
                <td><image src="<?php echo constant('SITE_URL_PATH').$row;?>" width="100" height="100"/></td>
              </tr>
              <?php endforeach;?>

              <tr>
              		<td colspan="3" align="center"><input type="submit" value="確認送出"/></td>
              </tr>
         	 </table>
         	 </form>
           </td>         
        </tr>
      </table>
    </td>
  </tr>
</table>
<?php  include("foot.php");?>